﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Ledict.Core.Data;
using System.Data.Common;
using Ledict.ConditionExtract.Model;
using Ledict.ConditionExtract.Common;

namespace Ledict.ConditionExtract.DAL
{
    public class Express_MatchSituationDAL
    {

        private string DbName;
        private string DbName1;

        public Express_MatchSituationDAL()
        {
            DbName = "JudgeDoc";
            DbName1 = "JudgeProcess";
        }

        /// <summary>
        /// 根据案由ID去获取组合数据
        /// </summary>
        /// <param name="ccId"></param>
        /// <returns></returns>
        public DataTable GetExpressWordData1(string ccId)
        {
            string sql = string.Format(@"   select MS_ID as id,SituationName as name,AllParentID from SYS_Express_MatchSituation where (SituationID='{0}' or SituationName like'%{0}%') and AllParentID like '1000%' and ParentID like'%EMS%' and IsMatch=1 and AffirmIdeaResult=1 ", ccId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }
        
        /// <summary>
        /// 根据案由ID去获取组合数据
        /// </summary>
        /// <param name="ccId"></param>
        /// <returns></returns>
        public DataTable GetExpressWordData1(string FirId, string queryNodeName)
        {
            string sql = string.Format(@"   select CC_ID  as id ,CC_Name as name ,AllParentId  as AllParentID from dbo.SYS_CaseCause where  charindex('{0}', AllParentId)>0 and(CC_Name like'%{1}%' or CC_ID = '{1}') and CC_NodeType=2 ", FirId, queryNodeName);


            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }
       
      
        /// <summary>
        /// 根据案由ID去获取数据
        /// </summary>
        /// <param name="ccId"></param>
        /// <returns></returns>
        public DataTable GetExpressWordData(string ccId)
        {
            string sql = string.Format(@"  select EW_ID,EW_Content as ParagraphText,EW_CaseId as CaseId,ParagraphTypeId as ParagraphTypeId from( 
 select EW_ID, EW_Content,EW_CaseId,ParagraphTypeId from Ldt_JudgeProcess.dbo.SYS_Express_Word_Process where MS_ID in( select CC_ID from SYS_CaseCause  where CHARINDEX(',{0},', ',' + AllParentId + ',') > 0)
 union  
 select EW_ID, EW_Content,EW_CaseId,ParagraphTypeId from  Ldt_JudgeProcess.dbo.SYS_Express_Word_Process where MS_ID in( select MS_ID from 	dbo.SYS_Express_MatchSituation where SituationID in( select CC_ID from SYS_CaseCause  where CHARINDEX(',{0},', ',' + AllParentId + ',') > 0 )) ) a 
", ccId);


            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            oCmd.CommandTimeout = 300000;
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }


        /// <summary>
        /// 根据父级Id去获取下级子级数据
        /// </summary>
        /// <param name="ccId"></param>
        /// <returns></returns>
        public DataTable GetExpressNodeData(string ccId)
        {
            string sql = string.Format(@" select MS_ID as id ,SituationName as name ,SituationType as CC_NodeType , isnull(IsMatch,2) as ES_IsMakeUp  
      , 1 as TableName from dbo.SYS_Express_MatchSituation
      where ParentID='{0}'"  , ccId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 根据父级Id去获取下级子级数据(表述库加工专用)
        /// </summary>
        /// <param name="ccId"></param>
        /// <returns></returns>
        public DataTable GetExpressNodeData1(string ccId)
        {
            string sql = string.Format(@" select MS_ID as id ,SituationName as name ,SituationType as CC_NodeType , isnull(IsMatch,2) as ES_IsMakeUp  
      , 1 as TableName from dbo.SYS_Express_MatchSituation
      where ParentID='{0}' and CC_IsShowExpress = 1 ", ccId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }
        

        /// <summary>
        /// 删除表述关系表内容和表述关系内容（争议焦点专用）
        /// </summary>
        /// <param name="cc_ID"></param>
        /// <returns></returns>
        public bool DeleteSys_ExpressAndWordZhengYI(string cc_ID)
        {
            try
            {
                string str = String.Format(" delete from Ldt_JudgeProcess.dbo.SYS_Express_Word_Process where MS_ID =(select MS_ID from Ldt_JudgeDocSystem.dbo.SYS_Express_MatchSituation where SituationID = '{0}') and EW_Right != 1; delete  from Ldt_JudgeProcess.dbo.SYS_Express_Word_Process where MS_ID in (select CC_ID from Ldt_JudgeDocSystem.dbo.SYS_CaseCause  where CHARINDEX(',{0},',','+AllParentId+',')>0) and EW_Right != 1 ;delete from Ldt_JudgeProcess.dbo.SYS_Express_Word_Process where MS_ID in (select MS_ID from Ldt_JudgeDocSystem.dbo.SYS_Express_MatchSituation where SituationID in(select CC_ID from Ldt_JudgeDocSystem.dbo.SYS_CaseCause  where CHARINDEX(',{0},',','+AllParentId+',')>0))and EW_Right != 1", cc_ID);
                Database oBase = DatabaseFactory.CreateDatabase(DbName1);
                DbCommand oCmd = oBase.GetSqlStringCommand(str);
                if (oBase.ExecuteNonQuery(oCmd) >= 0)
                {
                    return true;
                }
                return false;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 删除表述关系表内容和表述关系内容（争议焦点不包含本节点专用）
        /// </summary>
        /// <param name="cc_ID"></param>
        /// <returns></returns>
        public bool DeleteSys_ExpressNotZhengYi(string cc_ID)
        {
            try
            {
                string str = String.Format(" delete  from SYS_Express_Word_Process where MS_ID in (select CC_ID from Ldt_JudgeDocSystem.dbo.SYS_CaseCause  where CHARINDEX(',{0},',','+AllParentId+',')>0) and EW_Right != 1 ;delete  from SYS_Express_Word_Process where MS_ID in (select CC_ID from Ldt_JudgeDocSystem.dbo.SYS_CaseCause  where CHARINDEX(',{0},',','+AllParentId+',')>0) and EW_Right != 1 ;delete from Ldt_JudgeProcess.dbo.SYS_Express_Word_Process where MS_ID in (select MS_ID from Ldt_JudgeDocSystem.dbo.SYS_Express_MatchSituation where SituationID in(select CC_ID from Ldt_JudgeDocSystem.dbo.SYS_CaseCause  where CHARINDEX(',{0},',','+AllParentId+',')>0))and EW_Right != 1 ", cc_ID);
                Database oBase = DatabaseFactory.CreateDatabase(DbName1);
                DbCommand oCmd = oBase.GetSqlStringCommand(str);
                if (oBase.ExecuteNonQuery(oCmd) >= 0)
                {
                    return true;
                }
                return false;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        //public string GetMS_IDBySituationID(string SituationID, int IsMatch)
        //{
        //    Database oBase = DatabaseFactory.CreateDatabase(DbName);
        //    DbCommand oCmd = oBase.GetStoredProcCommand("sp_CondiExt_GetMS_IDBySituationID");
        //    oBase.AddInParameter(oCmd, "@SituationID", DbType.String, SituationID);
        //    oBase.AddInParameter(oCmd, "@IsMatch", DbType.Int32, IsMatch);
        //    return LdFormat.ObjectToString(oBase.ExecuteScalar(oCmd));
        //}
        /// <summary>
        /// 获取CC_ID信息
        /// </summary>
        /// <param name="CC_ID"></param>
        /// <returns></returns>
        public SYS_Express_MatchSituation GetMatchSituation(string MS_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select Top 1 * from SYS_Express_MatchSituation
	         where MS_ID=@MS_ID");

            SYS_CaseCause model = new SYS_CaseCause();
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@MS_ID", DbType.String, MS_ID);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return DataRowToModel(OtB.Rows[0]);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYS_Express_MatchSituation DataRowToModel(DataRow row)
        {
            SYS_Express_MatchSituation model = new SYS_Express_MatchSituation();
            if (row != null)
            {
                if (row["MS_ID"] != null)
                {
                    model.MS_ID = row["MS_ID"].ToString();
                }
                if (row["CauseGroupID"] != null)
                {
                    model.CauseGroupID = row["CauseGroupID"].ToString();
                }
                if (row["SituationID"] != null)
                {
                    model.SituationID = row["SituationID"].ToString();
                }
                if (row["SituationType"] != null && row["SituationType"].ToString() != "")
                {
                    model.SituationType = int.Parse(row["SituationType"].ToString());
                }
                if (row["SituationName"] != null)
                {
                    model.SituationName = row["SituationName"].ToString();
                }
                if (row["IsMatch"] != null && row["IsMatch"].ToString() != "")
                {
                    model.IsMatch = int.Parse(row["IsMatch"].ToString());
                }
                if (row["MatchSource"] != null)
                {
                    model.MatchSource = row["MatchSource"].ToString();
                }
                if (row["AffirmIdeaSource"] != null)
                {
                    model.AffirmIdeaSource = row["AffirmIdeaSource"].ToString();
                }
                if (row["AffirmIdeaResult"] != null)
                {
                    model.AffirmIdeaResult = row["AffirmIdeaResult"].ToString();
                }
                if (row["notAffirmReson"] != null && row["notAffirmReson"].ToString() != "")
                {
                    model.notAffirmReson = int.Parse(row["notAffirmReson"].ToString());
                }
                if (row["ParentID"] != null)
                {
                    model.ParentID = row["ParentID"].ToString();
                }
                if (row["AllStituationIDs"] != null)
                {
                    model.AllStituationIDs = row["AllStituationIDs"].ToString();
                }
                if (row["AllParentID"] != null)
                {
                    model.AllParentID = row["AllParentID"].ToString();
                }
                if (row["CC_IsShowExpress"] != null && row["CC_IsShowExpress"].ToString() != "")
                {
                    model.CC_IsShowExpress = int.Parse(row["CC_IsShowExpress"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 根据CCID获取是否存在SYS_Express_MatchSituation表中
        /// </summary>
        /// <param name="ccID"></param>
        /// <returns></returns>
        public string ExistsMatchSituationByCCID(string ccID)
        {
            try
            {
                string str = String.Format(" select MS_ID from SYS_Express_MatchSituation where SituationID = '{0}' ", ccID);
                Database oBase = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd = oBase.GetSqlStringCommand(str);
                return Convert.ToString(oBase.ExecuteScalar(oCmd));
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }     
        }

        // public string  AddExpress_MatchSituation(SYS_Express_MatchSituation  model   )
        //{
        //    Database oBase = DatabaseFactory.CreateDatabase(DbName);
        //    DbCommand oCmd = oBase.GetStoredProcCommand("sp_CondiExt_AddExpress_MatchSituation");
        //    oBase.AddInParameter(oCmd, "@CaseCauseID", DbType.String, model.CaseCauseID );
        //    oBase.AddInParameter(oCmd, "@SituationID", DbType.String, model.SituationID);
        //    oBase.AddInParameter(oCmd, "@IsMatch", DbType.Int32 , model.IsMatch);
        //    oBase.AddInParameter(oCmd, "@MatchSource", DbType.String, model.MatchSource);
        //    oBase.AddInParameter(oCmd, "@AffirmIdeaSource", DbType.String, model.AffirmIdeaSource);
        //    oBase.AddInParameter(oCmd, "@AffirmIdeaResult", DbType.String, model.AffirmIdeaResult);
        //    oBase.AddInParameter(oCmd, "@PlotDetail", DbType.String, model.PlotDetail);
        //    oBase.AddInParameter(oCmd, "@SerialNumb", DbType.Int32 , model.SerialNumb);
        //    oBase.AddInParameter(oCmd, "@SituationType", DbType.Int32, model.SituationType);
        //    return LdFormat.ObjectToString(oBase.ExecuteScalar(oCmd));

        //}
        /// <summary>
        /// 是否存在该记录 
        /// string msId = MatchSituationbll.Exists_MatchSituation(stiuid, 0, "0", "0", "0", stiuid,"0","");//存在
        /// </summary>
        public string Exists_MatchSituation(string stiuid, int IsMatch, string MatchSource, string AffirmIdeaResult, string AffirmIdeaSource, string AllStituationIDs,string parentId,string allParentId)
        {

            //string strSql = string.Format(@" select  MS_ID  from dbo.SYS_Express_MatchSituation
            //where
            //SituationID = '{0}'
            //and IsMatch = {1} and MatchSource = '{2}' and AffirmIdeaSource = '{3}' and AffirmIdeaResult = '{4}'

            //and AllStituationIDs = '{5}' and ParentID = '{6}'  ", stiuid, IsMatch, MatchSource, AffirmIdeaSource, AffirmIdeaResult, AllStituationIDs, parentId);

            string strSql = string.Format(@" select  MS_ID  from dbo.SYS_Express_MatchSituation
            where
            SituationID = '{0}'
            and IsMatch = {1} and MatchSource = '{2}' and AffirmIdeaSource = '{3}' and AffirmIdeaResult = '{4}'

            and AllStituationIDs = '{5}'  ", stiuid, IsMatch, MatchSource, AffirmIdeaSource, AffirmIdeaResult, AllStituationIDs);

            //    StringBuilder strSql = new StringBuilder();
            //    strSql.Append(@" select  MS_ID  from dbo.SYS_Express_MatchSituation
            //    where 
            //    SituationID=@SituationID
            //    and IsMatch=@IsMatch and MatchSource=@MatchSource and AffirmIdeaSource=@AffirmIdeaSource and AffirmIdeaResult=@AffirmIdeaResult 

            //    and AllStituationIDs=@AllStituationIDs and ParentID=@ParentID 


            //");
            if (!string.IsNullOrEmpty(allParentId))
            {
                strSql+=(" and AllParentID<>'-1'");
            }
            else
            {
                strSql += ("  and AllParentID='-1' ");
            }
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            //oBase.AddInParameter(oCmd, "@SituationID", DbType.String, stiuid);
            //oBase.AddInParameter(oCmd, "@IsMatch", DbType.Int32, IsMatch);
            //oBase.AddInParameter(oCmd, "@MatchSource", DbType.String, MatchSource);
            //oBase.AddInParameter(oCmd, "@AffirmIdeaSource", DbType.String, AffirmIdeaSource);
            //oBase.AddInParameter(oCmd, "@AffirmIdeaResult", DbType.String, AffirmIdeaResult);
            //oBase.AddInParameter(oCmd, "@AllStituationIDs", DbType.String, AllStituationIDs);
            //oBase.AddInParameter(oCmd, "@ParentID", DbType.String, parentId);
            return LdFormat.ObjectToString(oBase.ExecuteScalar(oCmd));
        }


        /// <summary>
        /// 是否存在该记录(民事数据处理用)
        /// </summary>
        public string Exists_MatchSituation1(string stiuid, int IsMatch, string MatchSource, string AffirmIdeaResult, string AffirmIdeaSource, string AllStituationIDs, string parentId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@" select  MS_ID  from dbo.SYS_Express_MatchSituation
            where 
            SituationID=@SituationID
            and IsMatch=@IsMatch and MatchSource=@MatchSource and AffirmIdeaSource=@AffirmIdeaSource and AffirmIdeaResult=@AffirmIdeaResult 

            and AllStituationIDs=@AllStituationIDs and ParentID=@ParentID 
             

        ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@SituationID", DbType.String, stiuid);
            oBase.AddInParameter(oCmd, "@IsMatch", DbType.Int32, IsMatch);
            oBase.AddInParameter(oCmd, "@MatchSource", DbType.String, MatchSource);
            oBase.AddInParameter(oCmd, "@AffirmIdeaSource", DbType.String, AffirmIdeaSource);
            oBase.AddInParameter(oCmd, "@AffirmIdeaResult", DbType.String, AffirmIdeaResult);
            oBase.AddInParameter(oCmd, "@AllStituationIDs", DbType.String, AllStituationIDs);
            oBase.AddInParameter(oCmd, "@ParentID", DbType.String, parentId);

            return LdFormat.ObjectToString(oBase.ExecuteScalar(oCmd));
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public string Exists_MatchSituation(string stiuid, int IsMatch, string MatchSource, string AffirmIdeaResult, string AffirmIdeaSource, string AllStituationIDs, string parentId, int ccIsShowExpress)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@" select  MS_ID  from dbo.SYS_Express_MatchSituation
            where 
            SituationID=@SituationID
            and IsMatch=@IsMatch and MatchSource=@MatchSource and AffirmIdeaSource=@AffirmIdeaSource and AffirmIdeaResult=@AffirmIdeaResult 

            and AllStituationIDs=@AllStituationIDs and ParentID=@ParentID and CC_IsShowExpress=@CC_IsShowExpress
             

        ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@SituationID", DbType.String, stiuid);
            oBase.AddInParameter(oCmd, "@IsMatch", DbType.Int32, IsMatch);
            oBase.AddInParameter(oCmd, "@MatchSource", DbType.String, MatchSource);
            oBase.AddInParameter(oCmd, "@AffirmIdeaSource", DbType.String, AffirmIdeaSource);
            oBase.AddInParameter(oCmd, "@AffirmIdeaResult", DbType.String, AffirmIdeaResult);
            oBase.AddInParameter(oCmd, "@AllStituationIDs", DbType.String, AllStituationIDs);
            oBase.AddInParameter(oCmd, "@ParentID", DbType.String, parentId);
            oBase.AddInParameter(oCmd, "@CC_IsShowExpress", DbType.Int32, ccIsShowExpress);



            return LdFormat.ObjectToString(oBase.ExecuteScalar(oCmd));
        }

        public string Exists_MatchSituation(int IsMatch, string AffirmIdeaResult, string AllStituationIDs)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@" select  MS_ID  from dbo.SYS_Express_MatchSituation
            where IsMatch=@IsMatch and AffirmIdeaResult=@AffirmIdeaResult and AllStituationIDs=@AllStituationIDs");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@IsMatch", DbType.Int32, IsMatch);
            oBase.AddInParameter(oCmd, "@AffirmIdeaResult", DbType.String, AffirmIdeaResult);
            oBase.AddInParameter(oCmd, "@AllStituationIDs", DbType.String, AllStituationIDs);

            return LdFormat.ObjectToString(oBase.ExecuteScalar(oCmd));
        }

        /// <summary>
        /// 是否存在该记录 2017-07-13 ljj 修改
        /// </summary>
        public string Exists_MatchSingleSituation1(int IsMatch, string AffirmIdeaResult, string stiuid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@" select  MS_ID  from dbo.SYS_Express_MatchSituation
            where 
            AllStituationIDs=@SituationID and SituationID=@SituationID  and AllParentID = '-1'    and    IsMatch=@IsMatch and AffirmIdeaResult=@AffirmIdeaResult   
        ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@SituationID", DbType.String, stiuid);
            oBase.AddInParameter(oCmd, "@IsMatch", DbType.String, IsMatch);
            oBase.AddInParameter(oCmd, "@AffirmIdeaResult", DbType.String, AffirmIdeaResult);

            return LdFormat.ObjectToString(oBase.ExecuteScalar(oCmd));
        }

        /// <summary>
        /// 获取本级及子节点数量
        /// </summary>
        /// <param name="stiuid"></param>
        /// <returns></returns>
        public string GetExpressMatchCount(string stiuid)
        {
            string strSql = string.Format("select SituationID from dbo.SYS_Express_MatchSituation where MS_ID = '{0}'",stiuid);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            string situationID= LdFormat.ObjectToString(oBase.ExecuteScalar(oCmd));

            strSql = string.Format(" select count(1) from ( select distinct AllStituationIDs   from SYS_Express_MatchSituation where AllStituationIDs like '{0},%' and AllParentID  <>'-1') a ", situationID);
             oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            return LdFormat.ObjectToString(oBase.ExecuteScalar(oCmd));
        }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool AddMatchSituation(SYS_Express_MatchSituation model)
        {
            int iResult = 0;

            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"
	    insert into SYS_Express_MatchSituation( MS_ID,CauseGroupID,SituationID,IsMatch,MatchSource,AffirmIdeaSource,AffirmIdeaResult,SituationType,SituationName,ParentID,AllStituationIDs,AllParentID,CC_IsShowExpress)
			values (@MS_ID,@CauseGroupID,@SituationID,@IsMatch,@MatchSource,@AffirmIdeaSource,@AffirmIdeaResult,@SituationType,@SituationName,@ParentID,@AllStituationIDs,@AllParentID,@CC_IsShowExpress)
			");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@MS_ID", DbType.String, model.MS_ID);
            oBase.AddInParameter(oCmd, "@CauseGroupID", DbType.String, model.CauseGroupID);
            oBase.AddInParameter(oCmd, "@SituationID", DbType.String, model.SituationID);
            oBase.AddInParameter(oCmd, "@IsMatch", DbType.Int32, model.IsMatch);
            oBase.AddInParameter(oCmd, "@MatchSource", DbType.String, model.MatchSource);
            oBase.AddInParameter(oCmd, "@AffirmIdeaSource", DbType.String, model.AffirmIdeaSource);
            oBase.AddInParameter(oCmd, "@AffirmIdeaResult", DbType.String, model.AffirmIdeaResult);

            oBase.AddInParameter(oCmd, "@SituationType", DbType.Int32, model.SituationType);
            oBase.AddInParameter(oCmd, "@SituationName", DbType.String, model.SituationName);
            oBase.AddInParameter(oCmd, "@ParentID", DbType.String, model.ParentID);

            oBase.AddInParameter(oCmd, "@AllStituationIDs", DbType.String, model.AllStituationIDs);
            oBase.AddInParameter(oCmd, "@AllParentID", DbType.String, model.AllParentID);

            oBase.AddInParameter(oCmd, "@CC_IsShowExpress", DbType.Int32, model.CC_IsShowExpress);
            try
            {
                iResult = oBase.ExecuteNonQuery(oCmd);
            }
            catch (Exception error)
            {
                AddLog.WriteLog(error, "");
            }
            return iResult > 0;

        }
        public bool AddMatchSituation(List<SYS_Express_MatchSituation> situationList)
        {
            StringBuilder strSql = new StringBuilder();
            foreach (var situation in situationList)
            {
                strSql.Append(string.Format(@"insert into SYS_Express_MatchSituation(MS_ID,CauseGroupID,SituationID,IsMatch,MatchSource,AffirmIdeaSource,AffirmIdeaResult,SituationType,SituationName,ParentID,AllStituationIDs,AllParentID,CC_IsShowExpress) values ('{0}','{1}','{2}',{3},'{4}','{5}','{6}',{7},'{8}','{9}','{10}','{11}',{12});",
                             situation.MS_ID, situation.CauseGroupID, situation.SituationID, situation.IsMatch, situation.MatchSource, situation.AffirmIdeaSource, situation.AffirmIdeaResult, situation.SituationType, situation.SituationName, situation.ParentID, situation.AllStituationIDs, situation.AllParentID, situation.CC_IsShowExpress));
            }
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            int iResult = 0;
            try
            {
                iResult = oBase.ExecuteNonQuery(oCmd);
            }
            catch (Exception error)
            {
                AddLog.WriteLog(error, "");
            }
            return iResult > 0;
            //DbConnection oConn = DatabaseFactory.CreateDatabase(DbName).CreateConnection();
            //oConn.Open();
            //DbTransaction dbtran = oConn.BeginTransaction();
            //DbCommand oCmd = oConn.CreateCommand();
            //oCmd.Transaction = dbtran;
            //try
            //{
            //    foreach (var situation in situationList)
            //    {
            //oCmd.CommandText = string.Format(@"
            //                      insert into SYS_Express_MatchSituation(MS_ID,CauseGroupID,SituationID,IsMatch,MatchSource,AffirmIdeaSource,AffirmIdeaResult,SituationType,SituationName,ParentID,AllStituationIDs,AllParentID,CC_IsShowExpress)
            //                values ('{0}','{1}','{2}',{3},'{4}','{5}','{6}',{7},'{8}','{9}','{10}','{11}',{12})
            //                   ",
            //                 situation.MS_ID, situation.CauseGroupID, situation.SituationID, situation.IsMatch, situation.MatchSource, situation.AffirmIdeaSource, situation.AffirmIdeaResult, situation.SituationType, situation.SituationName, situation.ParentID, situation.AllStituationIDs, situation.AllParentID, situation.CC_IsShowExpress);
            //        oCmd.ExecuteNonQuery();
            //    }
            //    dbtran.Commit();
            //    oConn.Close();
            //    return true;
            //}
            //catch (Exception error)//唯一键
            //{
            //    error.ToString();
            //    dbtran.Rollback();
            //    oConn.Close();
            //    return false;
            //}
        }
        /// <summary>
        /// 删除表述关系
        /// </summary>
        /// <param name="CC_ID">节点id</param>
        /// <returns></returns>
        public bool DeleteSYS_Express_MatchSituation(string CC_ID)
        {
            try
            {
                Database oBase = DatabaseFactory.CreateDatabase(DbName);

                ////查询匹配到的表述关联节点的所有父ID
                string strSql = string.Format(@"select MS_ID,AllParentID from SYS_Express_MatchSituation where MS_ID in(
                                                select MS_ID from dbo.SYS_Express_Word 
                                                where MS_ID in(select MS_ID from dbo.SYS_Express_MatchSituation 
                                                where SituationID in (select CC_ID from SYS_CaseCause  where CHARINDEX(',{0},',','+AllParentId+',')>0)))", CC_ID);
                DbCommand oCmd = oBase.GetSqlStringCommand(strSql);
                DataTable dt = oBase.ExecuteDataSet(oCmd).Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["AllParentID"].ToString() != "-1")
                    {
                        string[] arr = dt.Rows[i]["AllParentID"].ToString().Split(',');
                        for (int j = 0; j < arr.Length; j++)
                        {
                            if (dt.Select("MS_ID = '" + arr[j] + "'").Length == 0)
                            {
                                DataRow dr = dt.NewRow();
                                dr["MS_ID"] = arr[j];
                                dt.Rows.Add(dr);
                            }
                        }
                    }
                }
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    sb.AppendFormat("'{0}',", dt.Rows[i]["MS_ID"].ToString());
                }
                sb.Append("''");
                 strSql = string.Format(@"delete from SYS_Express_MatchSituation 
where SituationID in (select CC_ID from SYS_CaseCause  where CHARINDEX(',{0},',','+AllParentId+',')>0)
and MS_ID not in({1}) ", CC_ID, sb.ToString());
                oCmd = oBase.GetSqlStringCommand(strSql);
                oCmd.CommandTimeout = 300;
                if (oBase.ExecuteNonQuery(oCmd) >= 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 删除表述关系表内容和表述关系内容
        /// </summary>
        /// <param name="cc_ID"></param>
        /// <returns></returns>
        public bool DeleteSys_ExpressAndWord(string cc_ID)
        {
            try
            {
                string str = String.Format(" delete  from SYS_Express_MatchSituation where AllStituationIDs like '%{0}%';delete  from SYS_Express_Word_Process where EW_CaseId like '%{0}%' ", cc_ID);
                Database oBase = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd = oBase.GetSqlStringCommand(str);
                if (oBase.ExecuteNonQuery(oCmd) >= 0)
                {
                    return true;
                }
                return false;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 根据节点树界面修改名称后修改表述关系内容表节点名称
        /// 2018-03-07 需求
        /// </summary>
        /// <param name="oldCCName"></param>
        /// <param name="newCCName"></param>
        /// <param name="ccID"></param>
        /// <returns></returns>
        public bool UpdateCCName(string oldCCName, string newCCName,string ccID)
        {
            try
            {
                string str = string.Format(" update SYS_Express_MatchSituation set  SituationName=replace(SituationName,'{0}','{1}') where SituationID='{2}'",oldCCName,newCCName,ccID);
                Database oBase = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd = oBase.GetSqlStringCommand(str);
                if (oBase.ExecuteNonQuery(oCmd) >= 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }



        /// <summary>
        /// 根据案由ID去获取组合数据
        /// </summary>
        /// <param name="ccId"></param>
        /// <returns></returns>
        public DataTable GetExpressWordData2(string ccId)
        {
            string sql = string.Format(@" select * from SYS_Express_MatchSituation where SituationID='{0}'and AllStituationIDs='{0}'    ", ccId);
            Database oBase = DatabaseFactory.CreateDatabase("JudgeDoc3");
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 删除表述关系表内容和表述关系内容（争议焦点专用）
        /// </summary>
        /// <param name="cc_ID"></param>
        /// <returns></returns>
        public bool InsertMatchSituation(string msid)
        {
            try
            {
                string str = String.Format(" insert into Ldt_JudgeDocSystem.dbo.SYS_Express_MatchSituation  select * from Ldt_JudgeDocSystem220218.dbo.SYS_Express_MatchSituation where MS_ID='{0}'", msid);
                Database oBase = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd = oBase.GetSqlStringCommand(str);
                if (oBase.ExecuteNonQuery(oCmd) >= 0)
                {
                    return true;
                }
                return false;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 根据案由ID去获取组合数据
        /// </summary>
        /// <param name="ccId"></param>
        /// <returns></returns>
        public DataTable GetExpressData()
        {
            string sql = string.Format(@" SELECT CC_TextCRCCode  FROM [Ldt_JudgeDocSystem].[dbo].[_203个罪名案例表述_组合表述导表确定] group by CC_TextCRCCode    ");
            Database oBase = DatabaseFactory.CreateDatabase("JudgeDoc001");
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 根据案由ID去获取组合数据
        /// </summary>
        /// <param name="ccId"></param>
        /// <returns></returns>
        public DataTable GetExpressDataByCode(string code)
        {
            string sql = string.Format(@" SELECT * from [_203个罪名案例表述_组合表述导表确定] where CC_TextCRCCode='{0}' order by CauseId    ",code);
            Database oBase = DatabaseFactory.CreateDatabase("JudgeDoc001");
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

    }
}
